///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.describeType;
	
	import mx.utils.ObjectUtil;
	
	/**
	 * Does not work with generic timeline objects. Still not working as expected, visual quality of vector shape images decreases
	 * as if it was converted to BitmapData.
	 */
	public class DisplayObjectUtil {
		public static function clone(target:DisplayObject):DisplayObject {
			var targetClass:Class = Object(target).constructor;
			var clone:* = new targetClass() as DisplayObject;
			
			var i:int;
			var targetProps:XMLList = describeType(target).accessor;
			for (i=0;i<targetProps.length();i++) {
				if (targetProps[i].@access=="writeonly") continue; 
				if (!(target[targetProps[i].@name] is DisplayObject) && targetProps[i].@access=="readwrite") {
					clone[targetProps[i].@name] = target[targetProps[i].@name];
				}
			}
			
			var targetVars:XMLList = describeType(target).variable..@name;
			for (i=0;i<targetVars.length();i++) if (!(target[targetVars[i]] is DisplayObject)) clone[targetVars[i]] = target[targetVars[i]];
			
			//TODO: needs scale9grid fix
			
			//Flash 10 Only
			//if (target is Sprite || target is MovieClip) Sprite(clone).graphics.copyFrom(Sprite(target).graphics);
			
			return clone;
		}
	}
}